<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Examples</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../any.html" title="Chapter 4. Boost.Any 1.2">
<link rel="prev" href="../any.html" title="Chapter 4. Boost.Any 1.2">
<link rel="next" href="ValueType.html" title="ValueType requirements">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../any.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../any.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="ValueType.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="any.examples"></a><a class="link" href="examples.html" title="Examples">Examples</a>
</h2></div></div></div>
<p>
      The following code demonstrates the syntax for using implicit conversions to
      and copying of any objects:
    </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">list</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">any</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">any_cast</span><span class="special">;</span>
<span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">any</span><span class="special">&gt;</span> <span class="identifier">many</span><span class="special">;</span>

<span class="keyword">void</span> <span class="identifier">append_int</span><span class="special">(</span><span class="identifier">many</span> <span class="special">&amp;</span> <span class="identifier">values</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">value</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">any</span> <span class="identifier">to_append</span> <span class="special">=</span> <span class="identifier">value</span><span class="special">;</span>
    <span class="identifier">values</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">to_append</span><span class="special">);</span>
<span class="special">}</span>

<span class="keyword">void</span> <span class="identifier">append_string</span><span class="special">(</span><span class="identifier">many</span> <span class="special">&amp;</span> <span class="identifier">values</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;</span> <span class="identifier">value</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">values</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">value</span><span class="special">);</span>
<span class="special">}</span>

<span class="keyword">void</span> <span class="identifier">append_char_ptr</span><span class="special">(</span><span class="identifier">many</span> <span class="special">&amp;</span> <span class="identifier">values</span><span class="special">,</span> <span class="keyword">const</span> <span class="keyword">char</span> <span class="special">*</span> <span class="identifier">value</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">values</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">value</span><span class="special">);</span>
<span class="special">}</span>

<span class="keyword">void</span> <span class="identifier">append_any</span><span class="special">(</span><span class="identifier">many</span> <span class="special">&amp;</span> <span class="identifier">values</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">any</span> <span class="special">&amp;</span> <span class="identifier">value</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">values</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">value</span><span class="special">);</span>
<span class="special">}</span>

<span class="keyword">void</span> <span class="identifier">append_nothing</span><span class="special">(</span><span class="identifier">many</span> <span class="special">&amp;</span> <span class="identifier">values</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">values</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">any</span><span class="special">());</span>
<span class="special">}</span>
</pre>
<p>
      The following predicates follow on from the previous definitions and demonstrate
      the use of queries on any objects:
    </p>
<pre class="programlisting"><span class="keyword">bool</span> <span class="identifier">is_empty</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">any</span> <span class="special">&amp;</span> <span class="identifier">operand</span><span class="special">)</span>
<span class="special">{</span>
    <span class="keyword">return</span> <span class="identifier">operand</span><span class="special">.</span><span class="identifier">empty</span><span class="special">();</span>
<span class="special">}</span>

<span class="keyword">bool</span> <span class="identifier">is_int</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">any</span> <span class="special">&amp;</span> <span class="identifier">operand</span><span class="special">)</span>
<span class="special">{</span>
    <span class="keyword">return</span> <span class="identifier">operand</span><span class="special">.</span><span class="identifier">type</span><span class="special">()</span> <span class="special">==</span> <span class="keyword">typeid</span><span class="special">(</span><span class="keyword">int</span><span class="special">);</span>
<span class="special">}</span>

<span class="keyword">bool</span> <span class="identifier">is_char_ptr</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">any</span> <span class="special">&amp;</span> <span class="identifier">operand</span><span class="special">)</span>
<span class="special">{</span>
    <span class="keyword">try</span>
    <span class="special">{</span>
        <span class="identifier">any_cast</span><span class="special">&lt;</span><span class="keyword">const</span> <span class="keyword">char</span> <span class="special">*&gt;(</span><span class="identifier">operand</span><span class="special">);</span>
        <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span>
    <span class="special">}</span>
    <span class="keyword">catch</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bad_any_cast</span> <span class="special">&amp;)</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="keyword">false</span><span class="special">;</span>
    <span class="special">}</span>
<span class="special">}</span>

<span class="keyword">bool</span> <span class="identifier">is_string</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">any</span> <span class="special">&amp;</span> <span class="identifier">operand</span><span class="special">)</span>
<span class="special">{</span>
    <span class="keyword">return</span> <span class="identifier">any_cast</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;(&amp;</span><span class="identifier">operand</span><span class="special">);</span>
<span class="special">}</span>

<span class="keyword">void</span> <span class="identifier">count_all</span><span class="special">(</span><span class="identifier">many</span> <span class="special">&amp;</span> <span class="identifier">values</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span> <span class="special">&amp;</span> <span class="identifier">out</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">out</span> <span class="special">&lt;&lt;</span> <span class="string">"#empty == "</span>
        <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">count_if</span><span class="special">(</span><span class="identifier">values</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">values</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">is_empty</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
    <span class="identifier">out</span> <span class="special">&lt;&lt;</span> <span class="string">"#int == "</span>
        <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">count_if</span><span class="special">(</span><span class="identifier">values</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">values</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">is_int</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
    <span class="identifier">out</span> <span class="special">&lt;&lt;</span> <span class="string">"#const char * == "</span>
        <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">count_if</span><span class="special">(</span><span class="identifier">values</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">values</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">is_char_ptr</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
    <span class="identifier">out</span> <span class="special">&lt;&lt;</span> <span class="string">"#string == "</span>
        <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">count_if</span><span class="special">(</span><span class="identifier">values</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">values</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">is_string</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      The following type, patterned after the OMG's Property Service, defines name-value
      pairs for arbitrary value types:
    </p>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">property</span>
<span class="special">{</span>
    <span class="identifier">property</span><span class="special">();</span>
    <span class="identifier">property</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">&amp;,</span> <span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">any</span> <span class="special">&amp;);</span>

    <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">name</span><span class="special">;</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">any</span> <span class="identifier">value</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span><span class="special">&lt;</span><span class="identifier">property</span><span class="special">&gt;</span> <span class="identifier">properties</span><span class="special">;</span>
</pre>
<p>
      The following base class demonstrates one approach to runtime polymorphism
      based callbacks that also require arbitrary argument types. The absence of
      virtual member templates requires that different solutions have different trade-offs
      in terms of efficiency, safety, and generality. Using a checked variant type
      offers one approach:
    </p>
<pre class="programlisting"><span class="keyword">class</span> <span class="identifier">consumer</span>
<span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>
    <span class="keyword">virtual</span> <span class="keyword">void</span> <span class="identifier">notify</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">any</span> <span class="special">&amp;)</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
    <span class="special">...</span>
<span class="special">};</span>
</pre>
<p>
      The following code demonstrates <code class="computeroutput"><a class="link" href="../boost/anys/unique_any.html" title="Class unique_any">boost::anys::unique_any</a></code>
      usage for implementing HTTP processing with passing of optional user data from
      the authorizer to the HTTP body processing:
    </p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">HttpServer</span><span class="special">::</span><span class="identifier">ProcessRequest</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
    <span class="keyword">const</span> <span class="keyword">auto</span> <span class="identifier">path</span> <span class="special">=</span> <span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">ReceiveHttpStartingLine</span><span class="special">();</span>
    <span class="keyword">auto</span> <span class="identifier">headers</span> <span class="special">=</span> <span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">ReceiveHeaders</span><span class="special">();</span>

    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">anys</span><span class="special">::</span><span class="identifier">unique_any</span> <span class="identifier">user_data</span><span class="special">;</span>
    <span class="keyword">const</span> <span class="keyword">auto</span><span class="special">&amp;</span> <span class="identifier">user_authorizer</span> <span class="special">=</span> <span class="identifier">immutable_authorizers_</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span><span class="identifier">path</span><span class="special">);</span>
    <span class="keyword">if</span> <span class="special">(!</span><span class="identifier">user_authorizer</span><span class="special">(</span><span class="identifier">headers</span><span class="special">,</span> <span class="identifier">user_data</span><span class="special">))</span> <span class="special">{</span>
        <span class="keyword">throw</span> <span class="identifier">Unauthorized</span><span class="special">();</span>
    <span class="special">}</span>

    <span class="keyword">const</span> <span class="keyword">auto</span> <span class="identifier">body</span> <span class="special">=</span> <span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">ReceiveBody</span><span class="special">();</span>
    <span class="keyword">const</span> <span class="keyword">auto</span><span class="special">&amp;</span> <span class="identifier">user_handler</span> <span class="special">=</span> <span class="identifier">immutable_handlers_</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span><span class="keyword">this</span><span class="special">-&gt;</span><span class="identifier">ReceiveHttpStartingLine</span><span class="special">());</span>
    <span class="identifier">user_handler</span><span class="special">(</span><span class="identifier">body</span><span class="special">,</span> <span class="identifier">user_data</span><span class="special">);</span>
<span class="special">}</span>
</pre>
</div>
<div class="copyright-footer">Copyright © 2001 Kevlin Henney<br>Copyright © 2013-2024 Antony Polukhin<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../any.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../any.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="ValueType.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
